package com.guli.edu.controller;


import com.guli.commonUtils.R;
import com.guli.edu.entity.PayLog;
import com.guli.edu.service.impl.PayLogServiceImpl;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2023-04-26
 */
@ApiModel(value = "订单支付模块")
@RestController
@RequestMapping("/edu/order/payLog")
//@CrossOrigin // 解决跨域问题
public class PayLogController {

    @Autowired
    private PayLogServiceImpl payLogService;

    @ApiOperation(value = "生成微信支付的二维码")
    @GetMapping("/generateNative/{orderNo}")
    public R generateNative(
            @ApiParam(name = "orderNo" , value = "订单号" , required = true)
            @PathVariable String orderNo){
        Map map = payLogService.generateNative(orderNo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "获取该订单的支付状态")
    @GetMapping("/getPayStatus/{orderNo}")
    public R getPayStatus(
            @ApiParam(name = "orderNo" , value = "订单号" , required = true)
            @PathVariable String orderNo){
        Map<String , String> map = payLogService.queryPayStatus(orderNo);
        if(map == null){
            return R.error().message("支付服务崩溃了，未能支付成功~~~");
        }
        System.out.println("该订单的支付状态" + map.get("trade_state"));
        if(map.get("trade_state").equals("SUCCESS")){
            payLogService.addNewPayRecord(map);
            return R.ok().message("支付成功~~~");
        }
        return R.ok().code(25000).message("正在支付中，请耐心等待！！！");
    }
}

